package pl.vanta.mjuzik2.impl.playlists.types;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import pl.vanta.mjuzik2.api.AudioFile;
import pl.vanta.mjuzik2.impl.playlists.AbstractPlaylistType;

public class M3uPlaylistType extends AbstractPlaylistType
{
	private static final String	IDENTIFIER	= "#EXTM3U";

	@Override
	public String getDescription()
	{
		return "M3U files";
	}
	
	@Override
	public String[] getExtensions()
	{
		return new String[] { "m3u" };
	}

	private boolean acceptPlaylist(String firstLine)
	{
		return IDENTIFIER.equals(firstLine);
	}

	@Override
	public List<File> loadPlaylist(File playlist)
	{
		List<File> files = new ArrayList<File>();

		try
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(playlist), Charset
					.defaultCharset()));
			String line = br.readLine();

			if (acceptPlaylist(line))
			{
				while ((line = br.readLine()) != null)
				{
					if (line.startsWith("#"))
					{
						continue;
					}
					else if (line.trim().isEmpty())
					{
						continue;
					}
					else
					{
						File file = new File(line);

						if (file.exists())
						{
							if (file.isFile())
							{
								// normalny plik
								files.add(file);
							}
							else
							{
								// katalog
								files.addAll(Arrays.asList(file.listFiles()));
							}
						}
						else
						{
							// wzgledna sciezka
							file = new File(playlist.getParent() + File.separatorChar + line);

							if (file.exists())
							{
								if (file.isFile())
								{
									// normalny plik
									files.add(file);
								}
								else
								{
									// katalog
									files.addAll(Arrays.asList(file.listFiles()));
								}
							}
							else
							{
								// moze URL?
								try
								{
									URI uri = new URI(line);
									files.add(new File(uri));
								}
								catch (URISyntaxException e)
								{

								}
							}
						}
					}
				}

				return files;
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public String generatePlaylist(File file, List<AudioFile> audioFiles, boolean relative)
	{
		String prefix = "#EXTINF";

		StringBuilder sb = new StringBuilder();
		sb.append(IDENTIFIER);
		sb.append(NEW_LINE);

		String directory = file.getParent();
		if (directory.charAt(directory.length() - 1) != File.separatorChar)
		{
			directory += File.separatorChar;
		}

		for (AudioFile audioFile : audioFiles)
		{
			String path = audioFile.getFile().getParent() + File.separatorChar;

			if (relative)
			{
				int index = audioFile.getFile().getParent().indexOf(directory);

				if (index != -1)
				{
					path = audioFile.getFile().getParent().substring(index + directory.length()) + File.separatorChar;
				}
			}

			sb.append(prefix);
			sb.append(':');
			sb.append(audioFile.getFileInfo().getPlayTime() / 1000);
			sb.append(',');
			sb.append(audioFile.getFileInfo().getArtist());
			sb.append(" - ");
			sb.append(audioFile.getFileInfo().getTitle());
			sb.append(NEW_LINE);

			sb.append(path + audioFile.getFile().getName());
			sb.append(NEW_LINE);
		}

		return sb.toString();
	}
}